﻿using Adnc.Application.Shared.Services;
using Adnc.Infra.Helper;
using Adnc.Infra.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using WYT.Sys.Application.Contracts.Dtos.Common;
using WYT.Sys.Application.Contracts.Services;
using WYT.Sys.Repository.Entities;

namespace WYT.Sys.Application.Services
{
    public class CommonAppService : AbstractAppService, ICommonAppService
    {
        private readonly IEfRepository<Area> _areaEfRepository;

        private readonly IEfRepository<Banner> _bannerEfRepository;

        private readonly IEfRepository<Feedback> _feedbackEfRepository;

        public CommonAppService(IEfRepository<Area> areaEfRepository
            , IEfRepository<Banner> bannerEfRepository
            , IEfRepository<Feedback> feedbackEfRepository)
        {
            _areaEfRepository = areaEfRepository;
            _bannerEfRepository = bannerEfRepository;
            _feedbackEfRepository = feedbackEfRepository;
        }

        /// <summary>
        /// 获取下级地址信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<List<AreaDto>>> AreaQuery(AreaSearchDto input)
        {
            var result = await _areaEfRepository.Where(c => c.Parent_Code == input.Code && c.Level == input.Level)
                .Select(c => new AreaDto
                {
                    Code = c.Code,
                    Name = c.Name
                }).ToListAsync();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<List<BannerDto>>> GetBannerList(BannerSearchDto input)
        {
            return await _bannerEfRepository.Where(x => x.Location == input.Location && x.Equipment == input.Equipment && x.IsEnabled && (x.StartTime == null || x.StartTime <= DateTime.Now) && (x.EndTime == null || x.EndTime >= DateTime.Now))
                .OrderBy(x => x.Sort)
                .Select(x => new BannerDto
                {
                    Title = x.Title,
                    Type = x.Type,
                    ImgUrl = x.ImgUrl,
                    Target = x.Target,
                }).ToListAsync();
        }

        /// <summary>
        /// 提交反馈意见
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AppSrvResult> SubmitFeedBack(long userId, FeedbackDto input)
        {
            await _feedbackEfRepository.InsertAsync(new Feedback { 
                Id = IdGenerater.GetNextId(),
                Picture = JsonSerializer.Serialize(input.Picture),
                Describe = input.Describe,
                Mode = input.Mode,
                Tele = input.Tel,
                User = userId
            });

            return AppSrvResult();
        }
    }
}
